package com.rzjm.api.util;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.rendering.PDFRenderer;
import sun.net.util.URLUtil;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;

public class PdfToJpg {
    /**
     * 使用pdfbox将jpg转成pdf
     * @param jpgStream jpg输入流
     * @param pdfPath pdf文件存储路径
     * @throws IOException IOException
     */
    public static void jpgToPdf(InputStream jpgStream, String pdfPath) throws IOException {

        PDDocument pdDocument = new PDDocument();
        BufferedImage image = ImageIO.read(jpgStream);

        PDPage pdPage = new PDPage(new PDRectangle(image.getWidth(), image.getHeight()));
        pdDocument.addPage(pdPage);
        PDImageXObject pdImageXObject = LosslessFactory.createFromImage(pdDocument, image);
        PDPageContentStream contentStream = new PDPageContentStream(pdDocument, pdPage);
        contentStream.drawImage(pdImageXObject, 0, 0, image.getWidth(), image.getHeight());
        contentStream.close();
        pdDocument.save(pdfPath);
        pdDocument.close();
    }


//    public static void pdfbox() throws IOException {
//        long start = System.currentTimeMillis();
//        //pdf路径
//        URL url = new URL("file:///D:/1.pdf");
//        InputStream stream = URLUtil.getStream(url);
//        // 加载解析PDF文件
//        PDDocument doc = PDDocument.load(stream);
//        PDFRenderer pdfRenderer = new PDFRenderer(doc);
//        PDPageTree pages = doc.getPages();
//        int pageCount = pages.getCount();
//        for (int i = 0; i < pageCount; i++) {
//            BufferedImage bim = pdfRenderer.renderImageWithDPI(i, 200);
//            ByteArrayOutputStream os = new ByteArrayOutputStream();
//            ImageIO.write(bim, "jpg", os);
//            byte[] datas = os.toByteArray();
////            InputStream is = new ByteArrayInputStream(datas);
//            //jpg文件转出路径
//            FileUtil.writeBytes(datas, new File("d:/jpg/" + i + ".jpg"));
//        }
//        long end = System.currentTimeMillis();
//        long time = (end - start) / 1000;
//        System.out.println(StrUtil.format("pdf转jpg耗时： {}s", time));
//    }
//
//
//
//    /**
//     * 获取PDF总页数
//     * @throws IOException
//     */
//    public static int getPDFNum(String fileUrl) throws IOException {
//        PDDocument pdDocument = null;
//        int pages = 0;
//        try {
//            pdDocument = getPDDocument(fileUrl);
//            pages = pdDocument.getNumberOfPages();
//        } catch (Exception e) {
//            e.printStackTrace();
//            logger.error(e.getMessage(),e);
//        } finally {
//            if (pdDocument != null) {
//                pdDocument.close();
//            }
//        }
//        return pages;
//    }
//
//
//    /**
//     * PDF转图片 根据页码一页一页转
//     * @throws IOException
//     * imgType:转换后的图片类型 jpg,png
//     */
//    public static void PDFToImg(OutputStream sos,String fileUrl,int page,String imgType) throws IOException {
//        PDDocument pdDocument = null;
//        /* dpi越大转换后越清晰，相对转换速度越慢 */
//        int dpi = 100;
//        try {
//            pdDocument = getPDDocument(fileUrl);
//            PDFRenderer renderer = new PDFRenderer(pdDocument);
//            int pages = pdDocument.getNumberOfPages();
//            if (page <= pages && page > 0) {
//                BufferedImage image = renderer.renderImageWithDPI(page,dpi);
//                ImageIO.write(image, imgType, sos);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            logger.error(e.getMessage(),e);
//        } finally {
//            if (pdDocument != null) {
//                pdDocument.close();
//            }
//        }
//    }
//
//
//    private static PDDocument getPDDocument(String fileUrl) throws IOException {
//        File file = new File(fileUrl);
//        FileInputStream inputStream = new FileInputStream(file);
//        return PDDocument.load(inputStream);
//    }

}
